#include "Order.h"

Order::Order(string path){
    ifstream ifs;
    ifs.open(path, ios::in);

    int key = 0;
    string date;
    

    while(ifs>>date){
        map<string,vector<float>> map_date_buf;
        int pos = date.find("(");
        string order = date.substr(0,pos);
        string order_date = date.substr(pos+1,date.size() - pos - 2);

        vector<float> a;

        while((pos = order_date.find(",")) != -1){
            a.push_back(stof(order_date.substr(0,pos)));

            order_date = order_date.substr(pos+1,date.size() - pos - 1);
        }
        a.push_back(stof(order_date));


        map_date_buf.insert(make_pair(order,a));
        this->map_date.insert(make_pair(key,map_date_buf));
        
        key++;
    }
    
    this->m_Size = key;

    ifs.close();
}

/**
 * @brief 打印全部的指令
 * @author Dongcj (1217365417@qq.com)
 * @date 2025-08-19
*/
void Order::showOrder(){
    cout<<"共"<<this->m_Size<<"条指令"<<endl;
    for(auto it = this->map_date.begin(); it != this->map_date.end();it++){
        cout<<"第"<<it->first+1<<"条";

        for(auto mit = (*it).second.begin();mit!=it->second.end();mit++){
            cout<<" 指令为: "<<mit->first <<" 参数为: ";

            for (const auto& element : (*mit).second) {
                cout << element << " ";
            }
            cout << endl;
        }
    }
}

/**
 * @brief 执行指令
 * @author Dongcj (1217365417@qq.com)
 * @date 2025-08-19
*/
void Order::func(DevicePropeller &devicePropeller){
    for(auto it = this->map_date.begin(); it != this->map_date.end();it++){
        for(auto mit = (*it).second.begin();mit!=it->second.end();mit++){
            if(mit->first == "SAFE"){
                // 
                cout<<"this is SAFE"<<endl;

                this->maxtim = mit->second[0];
                this->maxdepth = mit->second[1];
                this->minpower = mit->second[2];
            }else if(mit->first == "DIVING"){
                cout<<"this is DIVING"<<endl;

                int time = mit->second[0];
                float depth = mit->second[1];
                float height = mit->second[2];

                usleep(this->maxtim*1000000);
                
                ToDepth(depth,devicePropeller);

            }else if(mit->first == "GALS"){
                cout<<"this is GALS"<<endl;

                int gals_time = mit->second[0];
                float heading = mit->second[1];
                float velocity = mit->second[2];
                int stab_mode = mit->second[3];
                float height_depth = mit->second[4];

            }
        }
        
    }

}

// void Order::updateOrder(){
    
// }